package com.awesome.imagedemo.bitmap;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.os.Bundle;
import android.widget.ImageView;

import com.awesome.imagedemo.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

public class BitmapActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);
        //图片的合并
        Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.head_img_mask);
        Bitmap dstBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.maopao);
        Bitmap newBitmap = BitmapUtil.getBimapForXfermode(srcBitmap, dstBitmap, PorterDuff.Mode.SRC_IN);
        ((ImageView) findViewById(R.id.imageview)).setImageBitmap(newBitmap);

        //色块的合并
//        Bitmap srcBitmap = makeSrc(400, 400);
//        Bitmap dstBitmap = makeDst(400, 400);
//        Bitmap newBitmap = BitmapUtil.getColorBlockForXfermode(srcBitmap, dstBitmap, PorterDuff.Mode.SRC_IN);
//        ((ImageView) findViewById(R.id.imageview)).setImageBitmap(newBitmap);

//        // Glide结合Xfermode裁切图片
//        Glide.with(this)
//                .asBitmap()
//                .load(R.mipmap.bg_src_morning)
//                .apply(RequestOptions.bitmapTransform(new CustomBitmapTransformation(PorterDuff.Mode.SRC_IN)))
//                .into((ImageView) findViewById(R.id.imageview));

//        // 高斯通过RenderScript实现
//        Bitmap blurBitmap = BitmapUtil.blurFormRenderScript(this,dstBitmap);
//        ((ImageView) findViewById(R.id.imageview)).setImageBitmap(blurBitmap);

//        // 高斯通过Glide实现
//        BitmapUtil.blurFormGlide((ImageView) findViewById(R.id.imageview),dstBitmap);
    }

    // create a bitmap with a circle, used for the "dst" image
    Bitmap makeSrc(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(0xFFFFCC44);
        c.drawOval(new RectF(0, 0, w * 3 / 4, h * 3 / 4), p);
        return bm;
    }

    // create a bitmap with a rect, used for the "src" image
    Bitmap makeDst(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(0xFF66AAFF);
        c.drawRect(w/3, h/3, w*19/20, h*19/20, p);
        return bm;
    }
}